﻿using Data;
using FastColoredTextBoxNS;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Codestyle
{
    class Codestyle
    {
        TextStyle GreenStyle = new TextStyle(Brushes.Green, null, FontStyle.Regular);
        TextStyle MediumVioletRedStyle = new TextStyle(Brushes.MediumVioletRed, null, FontStyle.Regular);
        TextStyle BlueStyle = new TextStyle(Brushes.Blue, null, FontStyle.Regular);
        TextStyle CornflowerBlueStyle = new TextStyle(Brushes.CornflowerBlue, null, FontStyle.Regular);
        TextStyle GrayStyle = new TextStyle(Brushes.Gray, null, FontStyle.Regular);
        TextStyle RedStyle = new TextStyle(Brushes.Red, null, FontStyle.Regular);


        public void StyleRun(object sender, FastColoredTextBoxNS.TextChangedEventArgs e)
        {

            /*FastColoredTextBoxNS.FastColoredTextBox fastColoredTextBox = (FastColoredTextBoxNS.FastColoredTextBox)sender;
            fastColoredTextBox.LeftBracket = '(';
            fastColoredTextBox.RightBracket = ')';
            fastColoredTextBox.LeftBracket2 = '{';
            fastColoredTextBox.RightBracket2 = '}';*/
            //fastColoredTextBox.LeftBracket2 = '\x0';
            //fastColoredTextBox.RightBracket2 = '\x0';

            //e.ChangedRange.ClearStyle(GreenStyle, MediumVioletRedStyle, BlueStyle, CornflowerBlueStyle, GrayStyle);
            //
            //e.ChangedRange.SetStyle(CornflowerBlueStyle, @"\$\{type\}|\$\{pageNo\}"); //匹配模版 ${}

            e.ChangedRange.SetStyle(MediumVioletRedStyle, @"\$\{.*?\}"); //匹配模版 ${}
            e.ChangedRange.SetStyle(GreenStyle, @""".*?""", RegexOptions.Multiline); //所有被""包裹的值
            e.ChangedRange.SetStyle(GreenStyle, @"'.*?'", RegexOptions.Multiline); //所有被''包裹的值
            e.ChangedRange.SetStyle(GreenStyle, @"<[^\/].*?>|<\/.*?>"); //所有被<></>包裹的值
            e.ChangedRange.SetStyle(BlueStyle, @"\$\..*?\(.*?\)"); //$. 内置函数
            e.ChangedRange.SetStyle(GrayStyle, @"/\*.*?\*/|/\*\s+.*?\s+\*/"); //注释
            e.ChangedRange.SetStyle(CornflowerBlueStyle, @"function");
            e.ChangedRange.SetStyle(MediumVioletRedStyle, @"import");
            e.ChangedRange.SetStyle(RedStyle, @"Main");
            e.ChangedRange.SetStyle(CornflowerBlueStyle, @"var");
            e.ChangedRange.SetStyle(CornflowerBlueStyle, @"return");
            e.ChangedRange.ClearFoldingMarkers();

            //代码块 区分
            e.ChangedRange.SetFoldingMarkers("{", "}");//allow to collapse brackets block
            e.ChangedRange.SetFoldingMarkers(@"\[", @"\]");
            e.ChangedRange.SetFoldingMarkers(@"<[^\/].*?>", @"<\/.*?>");
            e.ChangedRange.SetFoldingMarkers(@"/\*", @"\*/");
        }

        /// <summary>
        /// 提示框 
        /// </summary>
        AutocompleteMenu popupMenu;

        /// <summary>
        /// 初始化,代码高亮
        /// </summary>
        /// <param name="fastColoredTextBox"></param>
        public void Codehinting(FastColoredTextBoxNS.FastColoredTextBox fastColoredTextBox)
        {
            popupMenu = new AutocompleteMenu(fastColoredTextBox);
            //popupMenu.Items.ImageList = imageList1;
            popupMenu.SearchPattern = @"[\w\.:=!<>]";
            popupMenu.AllowTabKey = true;
            BuildAutocompleteMenu();
        }

        /// <summary>
        /// 代码提示
        /// </summary>
        private void BuildAutocompleteMenu()
        {
            //代码提示
            List<AutocompleteData> la = CodeAutoCompleteData.BuildAutocomplete();

            List<AutocompleteItem> items = new List<AutocompleteItem>();

            foreach (AutocompleteData item in la)
            {
                items.Add(new DeclarationSnippet(item.Snippets) { ImageIndex = 0, MenuText = item.MenuText, ToolTipTitle = item.ToolTipTitle, ToolTipText = item.ToolTipTitle });
            }

            //set as autocomplete source
            popupMenu.Items.SetAutocompleteItems(items);
           
        }

        /// <summary>
        /// 代码提示调用类
        /// </summary>
        class DeclarationSnippet : SnippetAutocompleteItem
        {
            public DeclarationSnippet(string snippet): base(snippet){}

            /// <summary>
            /// 循环被调用
            /// </summary>
            /// <param name="fragmentText"></param>
            /// <returns></returns>
            public override CompareResult Compare(string fragmentText)
            {
                var pattern = Regex.Escape(fragmentText);
                if (Regex.IsMatch(Text, "\\b" + pattern, RegexOptions.IgnoreCase)) {
                    return CompareResult.Visible;
                }                    
                return CompareResult.Hidden;
            }

        }
    }
}
